package model;

import java.util.Enumeration;
import java.util.Vector;

import dao.CategoryDAO;
import dao.RommDAO;

public class ListRoom {
	private Vector room;
	private RommDAO roomdao;
	
	private int id = 0;

	public ListRoom() {
		room= new Vector();
		
	}

	public void connect() {
		roomdao = new RommDAO();
		room=roomdao.getListroom();
	}



	public Vector getRoom() {

		return room;
	}

	public void setRoom(Vector room) {
		this.room = room;
	}

	public int getID() {
		return id;
	}

	public void addRoom(String category, long price, String description) {
		Room r = new Room(id, category, price, description);
		id++;
		room.addElement(r);
	}

	public void addRoom(Room r) {
		Room r1 = new Room(id, r.getType(), r.getPrice(), r.getDescription());
		id++;
		room.addElement(r1);
	}

	public ListRoom searchRoomAvailable(String type, long priceMin, long priceMax) {
		ListRoom result = new ListRoom();
		Enumeration r = room.elements();
		while (r.hasMoreElements()) {
			Room each = (Room) r.nextElement();
			if ((type == each.getType()) && (priceMin <= each.getPrice())
					&& (priceMax >= each.getPrice()) && (each.getStatus() == 0)) {
				result.addRoom(each);
			}
		}
		return result;
	}

	public ListRoom searchRoomByID(int id){
		ListRoom result = new ListRoom();
		Enumeration r = room.elements();
		while (r.hasMoreElements()) {
			Room each = (Room) r.nextElement();
			if (id == each.getID()) {
				result.addRoom(each);
			}
		}
		return result;
	}
	
	public ListRoom searchRoomByType(String type){
		ListRoom result = new ListRoom();
		Enumeration r = room.elements();
		while (r.hasMoreElements()) {
			Room each = (Room) r.nextElement();
			if (type == each.getType()) {
				result.addRoom(each);
			}
		}
		return result;
	}
	
	public ListRoom searchRoomByStatus(int status){
		ListRoom result = new ListRoom();
		Enumeration r = room.elements();
		while (r.hasMoreElements()) {
			Room each = (Room) r.nextElement();
			if (status == each.getStatus()) {
				result.addRoom(each);
			}
		}
		return result;
	}
	
	public ListRoom searchRoomByPrice(long priceMin, long priceMax) {
		ListRoom result = new ListRoom();
		Enumeration r = room.elements();
		while (r.hasMoreElements()) {
			Room each = (Room) r.nextElement();
			if ((priceMin <= each.getPrice()) && (priceMax >= each.getPrice())) {
				result.addRoom(each);
			}
		}
		return result;
	}
	
	public Room getRoomByID(int id) {
		Enumeration r = room.elements();
		while (r.hasMoreElements()) {
			Room each = (Room) r.nextElement();
			if (id == each.getID()) {
				return each;
			}
		}
		return new Room();
	}

	public void show() {
		System.out.println("List:");
		Enumeration r = room.elements();
		while (r.hasMoreElements()) {
			Room each = (Room) r.nextElement();
			System.out.println(each.toString());
		}
	}

	// update room ( chamge room information)

	// delele room
	public boolean deleteroom(int room_id) {
		int tmp = 0;
		boolean re = false;
		Enumeration r = room.elements();
		while (r.hasMoreElements()) {
			Room each = (Room) r.nextElement();
			if ((room_id == each.getID())) {
				while(r.hasMoreElements()){
					Room set = (Room) r.nextElement();
					if (room_id < set.getID()){
						tmp = set.getID();
						set.setRoom_id(tmp - 1);
					}
				}					
				room.remove(each);
				re = true;
			}
			
		}
		id--;
		return re;

	}

	// update room
	public boolean updateroom(Room room_id) {
		boolean re = false;
		Room each = null;
		int index = 0;
		Enumeration r = room.elements();
		while (r.hasMoreElements()) {
			each = (Room) r.nextElement();
			if ((id == each.getID())) {
				index = room.indexOf(each);
				break;
			}
		}
		if (each != null) {
			room.set(index, each);
			re = true;
		}

		return re;
	}

	public boolean set_statusRoom(int room_id, int status) {

		boolean re = false;
		Room each = null;
		int index = 0;
		Enumeration r = room.elements();
		while (r.hasMoreElements()) {
			each = (Room) r.nextElement();
			if ((id == each.getID())) {
				index = room.indexOf(each);
				each.setStatus(status);
				break;
			}
		}
		if (each != null) {
			room.set(index, each);
			re = true;
		}

		return re;
	}

	public Vector findby_Category(String category_id) {
		Vector rsult = new Vector();
		Enumeration re = getRoom().elements();
		while (re.hasMoreElements()) {
			Room each = (Room) re.nextElement();
			if ((category_id.equals(each.getCategory_id()))) {
				rsult.addElement(each);
				System.out.println("bang ko : " + category_id
						+ each.getCategory_id());
			}
		}
		System.out.println("serac" + rsult.size());
		return rsult;
	}
	

}